package com.dream.xiaobo.controller;

import com.dream.xiaobo.entity.Cart;
import com.dream.xiaobo.entity.User;
import com.dream.xiaobo.service.impl.CartServiceImpl;
import com.dream.xiaobo.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author xiaobo
 * @date 2021/9/1 - 9:34
 */
@WebServlet("/cart")
public class CartController extends BaseServlet{

    //创建cartService实例化对象
    private CartServiceImpl cartService = new CartServiceImpl();

    /**
     * 添加购物车模块
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String createCart(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //在Session中取出内容
        User user = (User) request.getSession().getAttribute("loginUser");
        //当取出的内容为空时
        if (user == null) {
            //返回提示信息
            request.setAttribute("msg","加入购物车必须登录！！！");
            //跳转到登录页面
            return Constants.FOR_WARD + "/login.jsp";
        }
        //获取到session中的用户Id
        Integer uId = user.getuId();
        //取出商品Id
        String pId = request.getParameter("pid");
        //调用调价购物车逻辑方法
        cartService.createCart(uId, pId);
        String tId = request.getParameter("tid");

        request.setAttribute("tId",tId);
        //跳转到调加购物车成功页面
        return Constants.FOR_WARD + "/cartSuccess.jsp";
    }

    /**
     * 显示购物车模块
     * @param request
     * @param response
     * @return
     */
    public String show(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //从Session中获取信息
        User user = (User) request.getSession().getAttribute("loginUser");
        //判断是否存在用户信息、否则重新登陆
        if (user == null) {
            //返回登录信息
            request.setAttribute("msg","加入购物车必须登录！！！");
            //跳转到登录页面
            return Constants.FOR_WARD + "/login.jsp";
        }
        //获取到用户Id
        Integer uId = user.getuId();
        //调用查询购物车中商品的逻辑方法
        List<Cart> list = cartService.findAll(uId);
        //将查询到的信息存起来返回
        request.setAttribute("list",list);
        //跳转到购物车页面
        return Constants.FOR_WARD + "/cart.jsp";
    }

    /**
     * 删除购物车模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String deleteCart(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //获取到购物车Id
        String cId = request.getParameter("cid");
        //调用购物车删除逻辑
        cartService.deleteCartByCid(cId);
        //跳转回显示购物车页面
        return Constants.FOR_WARD + "/cart?method=show";
    }

    /**
     * 更新购物车模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String updateCart(HttpServletRequest request, HttpServletResponse response) throws SQLException {

        //获取购物车Id
        String cId = request.getParameter("cid");
        //获取购物车商品的数量
        String cNum = request.getParameter("cnum");
        //获取购物车商品的价格
        String price = request.getParameter("price");
        //调用修改购物车逻辑
        cartService.updateCart(cId,cNum,price);
        //跳转回显示购物车页面
        return Constants.FOR_WARD + "cart?method=show";
    }

    /**
     * 清空购物车模块
     * @param request
     * @param response
     * @return
     * @throws SQLException
     */
    public String clear(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        //获取用户Id
        String uId = request.getParameter("uid");
        //调用清空购物车逻辑
        cartService.clearCart(uId);
        //跳转回显示购物车页面
        return Constants.FOR_WARD + "/cart?method=show";
    }
}
